<!doctype html>
<html>
<head>
<title></title>
<script type="text/javascript">
// 定义XMLHttpRequest对象
// 参数：无
// 返回值：XMLHttpRequest对象实例
function createXMLHTTPObject(){
    var XMLHttpFactories = [// 兼容不同浏览器和版本的创建函数数组
        function () {return new XMLHttpRequest()},
        function () {return new ActiveXObject("Msxml2.XMLHTTP")},
        function () {return new ActiveXObject("Msxml3.XMLHTTP")},
        function () {return new ActiveXObject("Microsoft.XMLHTTP")},
    ];
    var xmlhttp = false;
    for (var i = 0; i < XMLHttpFactories.length; i ++ ){ 
	//尝试调用匿名函数，如果成功则返回XMLHttpRequest对象，否则继续调用下一个
        try{
            xmlhttp = XMLHttpFactories[i]();
        }catch (e){
            continue; 			// 如果发生异常，则继续下一个函数调用
        }
        break; 					// 如果成功，则中止循环
    }
    return xmlhttp; 				// 返回对象实例
}
var xmlHttp = createXMLHTTPObject();

function request(url)
{
	xmlHttp.open("POST",url, false);
	xmlHttp.setRequestHeader('Content-type','text/xml');	
	xmlHttp.send("<bookstore><book id='1'>书名1</book><book id='2'>书名2</book></bookstore>");
	alert(xmlHttp.responseText);
}

window.onload = function()
{
    var b = document.getElementsByTagName("input")[0];
    b.onclick = function(){
        var url = "server2.asp"
        request(url);
    }   
}

</script>
<meta charset="utf-8">
</head>
<body>

<h1>Ajax异步数据传输</h1>
<input name="submit" type="button" id="submit" value="向服务器发出请求">

</body>
</html>
